<!--todo:需调整样式布局-->
<template>
  <MyCard style="height: 100%" v-if="show" :title="gradeUser.name+' 的试卷'" back>
    <div class="question_item" v-for="(item,index) in questionAndAnswerList">
      <p>{{index+1}}.&ensp;{{item.content}}.&ensp;({{item.score}}分)</p>
      <p>该考生提交的答案：{{ (userAnswerList[index] == undefined) ? "未作答" : userAnswerList[index].answerList }}</p>
      <p>正确答案：{{rightAnswerList[index]}}</p>
      <el-switch width="4em" inline-prompt v-model="determineList[index]" :active-text="'正确'" :inactive-text="'错误'" inactive-color="red" @change="temp"></el-switch>
    </div>
    <el-button style="width: 8em" type="primary" @click="commitDialog">提交</el-button>
  </MyCard>
  <el-dialog v-model="showCommitDialog" title="提交" width="30em">
    确定提交吗？提交后无法再次修改！
    学生的总分是：{{scoreSum}}
    <el-button @click="commit" type="primary">确定</el-button>
    <el-button @click="showCommitDialog=false" type="info">取消</el-button>
  </el-dialog>
  <el-dialog @close="router.back()" v-model="showFinishDialog" title="提示" width="30em">
    提交成功！
    <el-button @click="router.back()" type="primary">返回上一层</el-button>
  </el-dialog>
  <el-dialog v-model="showError" title="出现错误" width="35em">
    {{errorTxt}}
  </el-dialog>
</template>

<script setup lang="ts">
import MyCard from "../../../components/MyCard.vue";
import {Exam, Grade, Question, QuestionAndAnswer, QuestionAnswer, User, UserAnswerList} from "../../../types/mytypes";
import {PageStore} from "../../../store/store";
import {onMounted, reactive, ref} from "vue";
import {GradeRequest, QuestionRequest, UserRequest} from "../../../request/MyRequset";
import router from "../../../router/router";

const grade:Grade = PageStore.getNowGrade() as Grade;
const exam:Exam = PageStore.getNowExam() as Exam;
let gradeUser = ref<User>({} as User)
let questionAndAnswerList = ref<QuestionAndAnswer[]>([])
let userAnswerList = ref<UserAnswerList[]>([])
let rightAnswerList = ref<string[]>([])
//存储对应下标的题目是否正确的数组
let determineList = ref<boolean[]>([])
let show = ref(false)
let showCommitDialog = ref(false)
let showFinishDialog = ref(false)
let scoreSum = ref(0)
let errorTxt = ref("")
let showError = ref(false)

onMounted(async ()=>{
  //获取该成绩的考生
  gradeUser.value = (await UserRequest.getById(grade.u_id)).data;
  //获取该考生提交的所有答案
  let userAnswerRes = await QuestionRequest.getUserAllAnswer(gradeUser.value.id,exam.id);
  console.log(userAnswerRes)
  userAnswerList.value = userAnswerRes.data;
  //获取该考试所有题目
  let questionList:Question[] = (await QuestionRequest.getByExamId(exam.id)).data;
  //获取所有题目的所有答案
  for(let i in questionList){
    let q = questionList[i]
    let answerList:QuestionAnswer[] = (await QuestionRequest.getAllAnswer(q.id as number)).data;
    //向数组中存入数据
    questionAndAnswerList.value[i] = {
      id:q.id,
      answerList:answerList,
      content:q.content,
      type:q.type,
      u_id:gradeUser.value.id
    }
    //构建正确答案数组
    for(let j in answerList){
      if(answerList[j].isRight){
        rightAnswerList.value[i] = answerList[j].answer
      }
    }


    if(userAnswerList.value[i] == undefined){
      //学生这道题没做
      determineList.value[i] = false
    }else{
    }
  }
  show.value = true
})
const commitDialog = ()=>{
  scoreSum.value = statisticalScores();
  showCommitDialog.value=true
}
const commit = async ()=>{
  let pass:boolean = scoreSum.value >= exam.pass_score;
  const res = await GradeRequest.updateUserGrade(grade.id,scoreSum.value,true,pass)
  if(res.suc){
    showFinishDialog.value = true;
  }else{
    errorTxt.value = res.message;
    showError.value = true;
  }
}
//统计成绩
//fixme:待修改为新的题库写法，提示：score存于e_question中
const statisticalScores = ():number =>{
  let sum:number = 0;
  for(let i in questionAndAnswerList.value){
    let q = questionAndAnswerList.value[i]
    let d = determineList.value[i]
    if(d){
      // sum += q.score;
    }
  }
  return sum;
}
const temp = ()=>{
  PageStore.setTempDetermineList(determineList.value,grade.id)
}

</script>

<style scoped lang="scss">
.question_item{
  p{
    margin: 0;
    padding: 0;
  }
}
</style>